from __future__ import print_function

from keras.layers.recurrent import LSTM
from keras.models import Sequential, Graph
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.core import Activation, Dense, Flatten, Dropout, Reshape
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
from keras import backend as K


def scale(x):
    return (x - K.mean(x)) / K.std(x)
    # return x


def get_model():
    conv = Sequential()
    conv.add(Activation(activation=scale, input_shape=(30, 64, 64)))

    conv.add(Convolution2D(64, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(Convolution2D(64, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(MaxPooling2D(pool_size=(2, 2)))
    conv.add(BatchNormalization())
    conv.add(Dropout(0.5))

    conv.add(Convolution2D(128, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(Convolution2D(128, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(MaxPooling2D(pool_size=(2, 2)))
    conv.add(BatchNormalization())
    conv.add(Dropout(0.5))

    conv.add(Convolution2D(256, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(Convolution2D(256, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(MaxPooling2D(pool_size=(2, 2)))
    conv.add(BatchNormalization())
    conv.add(Dropout(0.5))

    conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(MaxPooling2D(pool_size=(2, 2)))
    conv.add(BatchNormalization())
    conv.add(Dropout(0.5))

    conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(MaxPooling2D(pool_size=(2, 2)))
    conv.add(BatchNormalization())
    conv.add(Dropout(0.5))

    conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    conv.add(Activation('relu'))
    conv.add(MaxPooling2D(pool_size=(2, 2)))
    conv.add(BatchNormalization())
    conv.add(Dropout(0.5))

    # conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    # conv.add(Activation('relu'))
    # conv.add(Convolution2D(512, 3, 3, border_mode='same'))
    # conv.add(Activation('relu'))
    # conv.add(MaxPooling2D(pool_size=(2, 2)))
    # conv.add(BatchNormalization())
    # conv.add(Dropout(0.5))

    conv.add(Flatten())

    meta = Sequential()
    meta.add(Dense(512, input_dim=4))
    meta.add(Activation('relu'))
    meta.add(Dropout(0.5))

    model = Graph()
    model.add_input(name='conv_input', input_shape=(30, 64, 64))
    model.add_input(name='meta_input', input_shape=(4,))
    model.add_node(conv, name='conv', input='conv_input')
    model.add_node(meta, name='meta', input='meta_input')

    model.add_node(Reshape((1, 1024)), name='merge_diastole', inputs=['conv', 'meta'], merge_mode='concat')
    model.add_node(LSTM(1024, return_sequences=True), name='lstm_input_diastole', input='merge_diastole')
    model.add_node(Dropout(0.55), name='merge_do_diastole', input='lstm_input_diastole')
    model.add_node(LSTM(1024, return_sequences=False), name='lstm_1_diastole', input='merge_do_diastole')
    model.add_node(Dropout(0.55), name='lstm_1_do_diastole', input='lstm_1_diastole')
    model.add_node(Dense(1), name='merge_out_diastole', input='lstm_1_do_diastole')
    model.add_output(name='output_diastole', input='merge_out_diastole')

    model.add_node(Reshape((1, 1024)), name='merge_systole', inputs=['conv', 'meta'], merge_mode='concat')
    model.add_node(LSTM(1024, return_sequences=True), name='lstm_input_systole', input='merge_systole')
    model.add_node(Dropout(0.55), name='merge_do_systole', input='lstm_input_systole')
    model.add_node(LSTM(1024, return_sequences=False), name='lstm_1_systole', input='merge_do_systole')
    model.add_node(Dropout(0.55), name='lstm_1_do_systole', input='lstm_1_systole')
    model.add_node(Dense(1), name='merge_out_systole', input='lstm_1_do_systole')
    model.add_output(name='output_systole', input='merge_out_systole')

    print(model.summary())
    return model
